(function(){
	HtmlClientUtil = {
		/**
		 * 获取所有的缓存参数，其结果为一个对象，其字段名为参数名，字段值为参数值
		 * 示例：{name: 'jake' ,age: 99}
		 */
	    getParamObject : function () {
	        var res = localCache.getAll();
			return res ? res : serviceCache.getAll();
	    }
		/**
		 * 获取指定key的参数值
		 * @param {Object} key
		 */
	    ,getParamValue: function (key) {
	        var res = localCache.get(key);
			return res ? res : serviceCache.get(key);
	    }
		/**
		 * 将指定数据设置到缓存中
		 * @param {Object} key
		 * @param {Object} value
		 */
		,setParamValue: function(key ,value){
			var paramObj = {}
			paramObj[key] = value
			HtmlClientUtil.setParamObjectValue(paramObj);
		}
		/**
		 * 将一个对象设置到缓存中，其对象的字段名为参数名，字段值为参数值；
		 * 示例：{name: 'jake' ,age: 99}
		 * @param {Object} paramObj
		 */
		,setParamObjectValue: function(paramObj){
			localCache.set(paramObj);
		},
		removeParamValue: function(key){
			localCache.removeOne(key);
			serviceCache.removeOne(key);
		},
		removeParamAll: function(){
			localCache.removeAll();
			serviceCache.removeAll();
		}
		/**
		 * 以post的方式跳转到指定url页面
		 * @param {Object} url 
		 * @param {Object} paramObj 请求参数
		 */
	    ,post: function post(url,paramObj){
	        var temp = document.createElement("form");
	        temp.action = url;
	        temp.method = "post";
	        temp.style.display = "none";
			if(paramObj){
				//本地缓存
				localCache.set(paramObj);
				for (var x in paramObj) {
					var opt = document.createElement("textarea");
					opt.name = x;
					opt.value = paramObj[x];
					temp.appendChild(opt);
				}
	        }
	        document.body.appendChild(temp);
	        temp.submit();
	    },
		/**
		 * 以get的方式跳转到指定url页面
		 * @param {Object} url
		 * @param {Object} paramObj
		 */
		get: function get(url ,paramObj){
			var lastUrl = url;
			if(paramObj){
				//本地缓存
				localCache.set(paramObj);
				var params = '';
				for (var x in paramObj) {
					params += x + '=' + paramObj[x] + '&';
				}
				lastUrl += '?' + encodeURI(params);
			}
			window.location.href = lastUrl;
		}
	};
	
	/**
	 * 本地数据管理
	 * 优先级： sessionStorage > localStorage > cookie > 过服务器缓存参数
	 * 
	 */
	var localCache = {
		set: function(paramObj){
			var wrapperKey = "LOCAL_TEMP_ARGS_KEY_0752" ,wrapperValue = '' ;
			//参数组装
			var oldParamObj = localCache.getAll();
			var newParamObj = oldParamObj ? oldParamObj : {}
			for(var param in paramObj){
				newParamObj[param] = paramObj[param];
			}
			//转化为json字符串
			var wrapperValue = JSON.stringify(newParamObj);
		
			//参数存储
			if(window.sessionStorage){
				window.sessionStorage.setItem(wrapperKey ,wrapperValue);
			}
			if(window.localStorage){
				window.localStorage.setItem(wrapperKey ,wrapperValue);
			}
			if(window.cookie){
				cookie.set(wrapperKey ,wrapperValue ,1);
			}
		},
		getAll: function(){
			var wrapperKey = "LOCAL_TEMP_ARGS_KEY_0752";
			var wrapperValue = window.sessionStorage.getItem(wrapperKey);
			!wrapperValue ? (wrapperValue = window.localStorage.getItem(wrapperKey)) : '';
			!wrapperValue ? (wrapperValue = cookie.get(wrapperKey)) : '';
			if(wrapperValue){
				try{
					return JSON.parse(wrapperValue);
				}catch(e){
					//不做处理
				}
			}
		},
		get: function(name){
			var paramObj = localCache.getAll();
			if(paramObj){
				return paramObj[name];
			}
		},
		removeOne: function(name){
			var paramObj = localCache.getAll();
			if(paramObj){
				var newParamObj = {};
				for(var key in paramObj){
					if(name != key){
						newParamObj[key] = paramObj[key];
					}
				}
				//删除之前的
				localCache.removeAll();
				//缓存新的
				localCache.set(newParamObj);
			}
		},
		removeAll: function(){
			var wrapperKey = "LOCAL_TEMP_ARGS_KEY_0752";
			//参数存储
			if(window.sessionStorage){
				window.sessionStorage.removeItem(wrapperKey);
			}else if(window.localStorage){
				window.localStorage.removeItem(wrapperKey);
			}else if(window.cookie){
				cookie.delete(wrapperKey);
			}
		}
	}
	/**
	 * 服务器缓存
	 */
	var serviceCache = {
		getAll: function(){
			var res = {}
			var paramElemArray = document.getElementsByClassName("inContainerSetting-class");
			for(var i = 0 ;i < paramElemArray.length ;++i){
			    var paramElem = paramElemArray[i];
				res[paramElem.getAttribute("name")] = paramElem.getAttribute("value")
			}
			return res;
		},
		get: function(key){
			var elem = document.getElementById('inContainerSetting-' + key);
			return elem ? elem.getAttribute("value") : undefined;
		},
		removeOne: function(key){
			var elem = document.getElementById('inContainerSetting-' + key);
			elem ? elem.remove() : '';
		},
		removeAll: function(){
			var paramElemArray = document.getElementsByClassName("inContainerSetting-class");
			for(var i = 0 ;i < paramElemArray.length ;++i){
			   paramElemArray[i].remove();
			}
		}
	}
	
	var cookie = {
	    set:function(key,val,dayTime){//设置cookie方法
	        var date=new Date(); //获取当前时间
	        var expiresDays=dayTime;  //将date设置为n天以后的时间
	        date.setTime(date.getTime()+expiresDays*24*3600*1000); //格式化为cookie识别的时间
	        document.cookie=key + "=" + val +";expires="+date.toGMTString();  //设置cookie
	    },
	    get:function(key){//获取cookie方法
	        /*获取cookie参数*/
	        var getCookie = document.cookie.replace(/[ ]/g,"");  //获取cookie，并且将获得的cookie格式化，去掉空格字符
	        var arrCookie = getCookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
	        var tips;  //声明变量tips
	        for(var i=0;i<arrCookie.length;i++){   //使用for循环查找cookie中的tips变量
	            var arr=arrCookie[i].split("=");   //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
	            if(key==arr[0]){  //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
	                tips=arr[1];   //将cookie的值赋给变量tips
	                break;   //终止for循环遍历
	            }
	        }
	        return tips;
	    },
		delete:function(key){ //删除cookie方法
		 var date = new Date(); //获取当前时间
		 date.setTime(date.getTime()-10000); //将date设置为过去的时间
		 document.cookie = key + "=v; expires =" +date.toGMTString();//设置cookie
		}
	}
})();